﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Nate_sRoguelikeGameXNA
{
    abstract class Screen
    {
        # region Fields

        protected KeyStroke _lastKeystroke;
        protected SpriteBatch _spriteBatch;
        protected delegate void InputHandler();

        protected InputHandler _inputHandler;

        # endregion

        # region Constructor

        protected Screen(SpriteBatch spriteBatch)
        {
            _spriteBatch = spriteBatch;
            _inputHandler = HandleInput;
        }

        #endregion
        # region Methods

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public abstract void Draw(GameTime gameTime);

        protected virtual bool GetInput(GameTime currentGameTime)
        {
            var currentKeystroke = new KeyStroke(Keyboard.GetState().GetPressedKeys(), currentGameTime);

            if (currentKeystroke.Key == Keys.None)
            {
                _lastKeystroke = currentKeystroke;
                return false;
            }
            else
                if (currentKeystroke.Equals(_lastKeystroke))
                {
                    if (_lastKeystroke.IsRepeatAllowed(currentGameTime))
                        _inputHandler();
                }
                else
                {
                    _lastKeystroke = currentKeystroke;

                    _inputHandler();
                }
            return true;
        }

        protected abstract void HandleInput();

        public abstract void Initialize();

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public abstract void Update(GameTime gameTime);

        # endregion

    }
}
